define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bdl/pur/porder/index' + location.search,
                    export_url: 'bdl/pur/porder/export',
                    table: 'pur_order',
                }
            });

            var table       = $("#table");
            var showExport  = $(".btn-export").data('value')

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                search:false,
                showFooter:true,
                fixedColumns: true,
                fixedRightNumber: 1,
                showToggle: false,
                showExport: showExport,
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID',operate:false},
                        {
                            field: 'forgid', title: __('数据中心/组织'), searchList: function (column) {
                                return Template('forgidtpl', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'fenable', title: '使用状态', searchList: {"1":'可用',"0":'禁用'}, formatter: Table.api.formatter.status},
                        {field: 'fk_bdl_comment', title: '下单备注', operate: 'LIKE'},
                        {field: 'fk_bdl_allqty', title: '总数量',operate:'BETWEEN',  autocomplete:false,totalRow: true},

                        {field: 'fk_bdl_erpbillno', title: 'ERP订单号', operate: 'LIKE'},

                        {field: 'fk_bdl_orderstatus', title: '订单状态', searchList: {"1":'已下单',"2":'装货中',"3":'配送中',"4":'已签收',"5":'已退货',"6":'待评价'}, formatter: Table.api.formatter.status},

                        {field: 'fk_bdl_bd_useraddress', title: '发货地址ID', operate: 'LIKE'},
                        {field: 'fk_bdl_bd_useraddress_mes', title: '发货地址信息',formatter: function (value,row_data)
                            {
                                var str = '';
                                if(row_data.address)
                                {
                                     str = `收货人: ${row_data.address.bdl_contacts} 联系方式: ${row_data.address.fk_bdl_phone}<br>`;
                                    str += `${row_data.address.fk_bdl_province}${row_data.address.fk_bdl_city}${row_data.address.fk_bdl_region}<br>`;
                                    str += `详细地址 ： ${row_data.address.fk_bdl_bd_cusaddress}`;
                                }else{
                                     str = '无';
                                }

                                // {$item.bdl_contacts} {$item.fk_bdl_phone} {$item.fk_bdl_province} {$item.fk_bdl_city} {$item.fk_bdl_region}
                                return `<a  href="javascript:void(0);"  >${str}</a>`
                            }},

                        {field: 'fk_bdl_bd_carinfo', title: '车辆信息ID', operate: 'LIKE'},
                        {field: 'fk_bdl_bd_customer', title: '客户ID', operate: 'LIKE'},
                        {field: 'cust.fname', title: '客户名称', operate: 'LIKE'},


                        {field: 'fk_bdl_allamount', title: '总金额',operate:'BETWEEN',  autocomplete:false,totalRow: true},

                        {field: 'fk_bdl_ordertime', title: '客户下单时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'fk_bdl_confirmtime', title: '业务员确认时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'fk_bdl_planshiptime', title: '预计发货时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'fk_bdl_signtime', title: '签收时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'fk_bdl_planreceivetime', title: '预计送达时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'fk_bdl_returntime', title: '退款时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'fk_bdl_date', title: '业务日期--销售订单业务日期', operate:'RANGE', addclass:'datetimerange', autocomplete:false},

                        {field: 'fk_bdl_signattach', title: '签收单图片', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'fk_bdl_materialbill', title: '产品清单（单据）', operate: 'LIKE'},
                        {field: 'fk_bdl_bizpersonlevel', title: '业务员评价等级', operate: 'LIKE'},
                        {field: 'fk_bdl_carinfolevel', title: '配送车辆评价等级', operate: 'LIKE'},
                        {field: 'fk_bdl_evaluateinfo', title: '评价内容', operate: 'LIKE'},




                        {field: 'datacenter.fname', title: '数据中心名称',operate: false},
                        {field: 'forg.fname', title: '组织名称',operate: false},
                        {field: 'forg.fnumber', title: '组织编码',operate: false},
                        {field: 'forg.id', title: '组织ID',operate: false},

                        // {
                        //     field: '12321',
                        //     title: __('111111111111111111'),
                        //     table: table,
                        //     events: Table.api.events.operate,
                        //     formatter: Table.api.formatter.operate,
                        //     buttons: [
                        //         {
                        //             name: 'edit',
                        //             text: '详情',
                        //             icon: 'fa fa-eye',
                        //             title: __('详情'),
                        //             extend: 'data-toggle="tooltip"',
                        //             classname: 'btn btn-xs btn-success btn-editone',
                        //             visible: function (row) {
                        //                 // 删除按钮 动态是否显示
                        //                 if (row.id == '合计') {
                        //                     return false;
                        //                 }
                        //                 return true;
                        //             }
                        //         },
                        //     ],
                        // },
                        {field: 'fk_bdl_createtime', title: '创建时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'fk_bdl_modifytime', title: '修改时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                name: 'detail',
                                text: __('详细信息'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'bdl/pur/porder/look'
                            }
                            ],
                            formatter: Table.api.formatter.operate}

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        action: function ()
        {
            $(document).ready(function(){
                $(document).on('change','#c-fk_bdl_orderstatus',function(){
                    var that = $(this)
                    var val  = that.val()

                    // alert(val)
                    if(val == 1)
                    {
                        $(".orderstatus2").addClass('hide')
                        $(".orderstatus3").addClass('hide')
                        $(".orderstatus4").addClass('hide')
                    }
                    if(val == 2)
                    {
                        $(".orderstatus2").removeClass('hide')
                        $(".orderstatus3").addClass('hide')
                        $(".orderstatus4").addClass('hide')
                    }
                    if(val == 3)
                    {
                        $(".orderstatus2").removeClass('hide')
                        $(".orderstatus3").removeClass('hide')
                        $(".orderstatus4").addClass('hide')

                    }
                    if(val == 4)
                    {
                        $(".orderstatus2").removeClass('hide')
                        $(".orderstatus3").removeClass('hide')
                        $(".orderstatus4").removeClass('hide')

                    }

                })
            })

            Controller.api.bindevent();
        },
        look: function ()
        {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    return Controller;
});
